package cn.school.common;

import cn.school.entity.Subject;
import cn.school.service.SubjectService;
import cn.school.util.StringUtil;
import cn.school.vo.Page;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author oldliu
 * @since 1.0
 */
public class SubjectController {
    public void add(HttpServletResponse response, HttpServletRequest req) throws ServletException, IOException {
        Long gradeId = Long.valueOf(req.getParameter("gradeid"));
        String subjectname = req.getParameter("subjectname");
        int classhour = Integer.parseInt(req.getParameter("classhour"));

        Subject subject = new Subject();
        subject.setGradeId(gradeId);
        subject.setSubjectname(subjectname);
        subject.setClasshour(classhour);
        int add = SubjectService.INST.add(subject);
        response.getWriter().print(add);
    }
    public void get(HttpServletResponse response, HttpServletRequest req) throws ServletException, IOException {
        Long id = Long.valueOf(req.getParameter("id"));

        Subject subject = SubjectService.INST.get(id);

        response.getWriter().print(JSON.toJSON(subject));
    }
    public String del(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        response.getWriter().print("这是一个del Subject的方法！");
        return null;
    }
    public void update(HttpServletResponse response, HttpServletRequest req) throws ServletException, IOException {
        Long id = Long.valueOf(req.getParameter("id"));
        Long gradeId = Long.valueOf(req.getParameter("gradeid"));
        String subjectname = req.getParameter("subjectname");
        int classhour = Integer.parseInt(req.getParameter("classhour"));

        Subject subject = new Subject();
        subject.setId(id);
        subject.setGradeId(gradeId);
        subject.setSubjectname(subjectname);
        subject.setClasshour(classhour);
        int add = SubjectService.INST.update(subject);
        response.getWriter().print(add);
    }
    public void search(HttpServletResponse response, HttpServletRequest request) throws ServletException, IOException {
        int pageNo= StringUtil.toInt(request.getParameter("pageNo"),1);
        String sname=request.getParameter("subjectname");
        Page<Subject> page = SubjectService.INST.search(pageNo, Page.PAGE_SIZE, sname);
        String str = JSON.toJSONString(page);

        response.getWriter().print(str);
    }
}
